#---- Load libraries and funtions -----

library(haven)
library(dplyr)
library(tidyverse)
library(broom)
library(purrr)
library(readxl)
library(writexl)
library(gridExtra)
library(srvyr)
library(sjstats)
library(weights)
library(broom)
library(sjPlot)

options(scipen=999)# non-scientific notation


#reading data#--------------------------------------------

data <- as.data.frame(read_sav('EU30_ALL_2022.sav'))


#variable transformations

data$random<-as.factor(data$random)
data$awareness<-as.numeric(data$awareness)
data$sceptical<-as.numeric(data$sceptical)
data$worry<-as.numeric(data$worry)
data$impact<-as.numeric(data$impact)

##recoding left-right

data <- data %>% 
  mutate(leftright=case_when(left_right<4 ~ 1,
                             left_right %in% 4:6 ~ 2,
                             left_right %in% 7:10 ~3,
                             left_right==99 ~ 99))

##recoding green and right-wing

data <- data %>% 
  mutate(green_right=case_when(green==1 ~ 1,
                             rightwing==1 ~ 2,
                             TRUE ~ 0))


##cntry labels

cntry_labels <- c("1" = "Austria", "2" = "Belgium", "3" = "Bulgaria", "4" = "Croatia", "5"="Cyprus",
                  "6" = "Czech Rep.", "7" = "Denmark", "8"= "Estonia", "9"="Finland", "10" = "France",
                  "11" = "Germany","12" = "Greece", "13"="Hungary", "14"="Ireland", "15"="Italy", "16"="Latvia", "17"="Lithuania", 
                  "18"="Luxembourg", "19"="Malta", "20"="Netherlands", "21"="Poland", "22"="Portugal", "23"="Romania", 
                  "24"="Slovakia", "25"="Slovenia", "26"="Spain", "27"="Sweden", 
                  "28"="United Kingdom","36"="Norway","37"="Switzerland","40"="Hungary")

##calculating mean_sd between the conditions - full sample


aw <- data %>%
  group_by(random) %>%
  summarise(mean=weighted.mean(awareness, weight_post,na.rm=T),
            sd=sqrt(wtd.var(awareness,weight_post,na.rm=T))) %>%
  mutate(question="Awareness")
aw
scep <- data %>%
  group_by(random) %>%
  summarise(mean=weighted.mean(sceptical, weight_post,na.rm=T),
            sd=sqrt(wtd.var(sceptical,weight_post,na.rm=T))) %>%
  mutate(question="Attr.scep.")
scep

worry <- data %>%
  group_by(random) %>%
  summarise(mean=weighted.mean(worry, weight_post,na.rm=T),
            sd=sqrt(wtd.var(worry,weight_post,na.rm=T))) %>%
  mutate(question="Worry")

worry

impact <- data %>%
  group_by(random) %>%
  summarise(mean=weighted.mean(impact, weight_post,na.rm=T),
            sd=sqrt(wtd.var(impact,weight_post,na.rm=T))) %>%
  mutate(question="Impact")
impact

mean_sd <- aw %>% #creating table for plotting
  full_join(scep) %>%
  full_join(worry) %>%
  full_join(impact)
mean_sd

mean_sd$question <- factor(mean_sd$question, levels=c("Awareness",##fixing order
                                                      "Attr.scep.",
                                                      "Worry","Impact"))
##running t tests
weighted_ttest(awareness ~ random +weight_post,data)
weighted_ttest(sceptical ~ random +weight_post,data)
weighted_ttest(worry ~ random +weight_post,data)
weighted_ttest(impact ~ random +weight_post,data)

###calculating ns
sum(complete.cases(data$awareness))
sum(complete.cases(data$sceptical))
sum(complete.cases(data$worry))
sum(complete.cases(data$impact))

###plotting means

p1 <- ggplot(subset(mean_sd,question=='Awareness'),aes(random,mean,fill=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.6) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",
                    labels=c("Global warming","Climate change"))+
  scale_y_continuous(limits = c(1, 5.5), 
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"))+
  geom_text(x=1.5, y=5, label="p=.000",color="grey20",family="Times New Roman",size=5)+
  theme_minimal()+
  ggtitle("Existence belief")+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5,size=17),
        text=element_text(family="Times New Roman",size=17),
        axis.text.y=element_text(size=12))
p1

p2 <- ggplot(subset(mean_sd,question=='Attr.scep.'),aes(random,mean,fill=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.6) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",
                   labels=c("Global warming","Climate change"))+
  scale_y_continuous(name="",limits = c(0, 6.5), 
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"))+
  geom_text(x=1.5, y=6, label="p=.973",color="grey20",family="Times New Roman",size=5)+
  theme_minimal()+
  ggtitle("Attribution scepticism")+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5,size=17),
        text=element_text(family="Times New Roman",size=17),
        axis.text.y=element_text(size=12))
p2

p3 <- ggplot(subset(mean_sd,question=='Worry'),aes(random,mean,fill=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.6) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",
                   labels=c("Global warming","Climate change"))+
  scale_y_continuous(limits = c(1, 5.5), 
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"))+
  geom_text(x=1.5, y=5, label="p=.538",color="grey20",family="Times New Roman",size=5)+
  theme_minimal()+
  ggtitle("Worry")+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5,size=17),
        text=element_text(family="Times New Roman",size=17),
        axis.text.y=element_text(size=12))

p3

p4 <- ggplot(subset(mean_sd,question=='Impact'),aes(random,mean,fill=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.6) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",
                   labels=c("Global warming","Climate change"))+
  scale_y_continuous(name="",limits = c(0, 10), 
                     oob = scales::squish,labels=scales::number_format(accuracy = 1))+
  scale_fill_manual(values=c("grey80","grey40"))+
  geom_text(x=1.5, y=9, label="p=.058",color="grey20",family="Times New Roman",size=5)+
  theme_minimal()+
  ggtitle("Negative impact")+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5,size=17),
        text=element_text(family="Times New Roman",size=17),
        axis.text.y=element_text(size=12))

p4
p<-grid.arrange(p1,p2,p3,p4,nrow=2)
p
ggsave("fig1.png",p,h=7)

##calculating mean_sd between the conditions - left-right


aw <- data %>%
  group_by(leftright,random) %>%
  summarise(mean=weighted.mean(awareness, weight_post,na.rm=T),
            sd=sqrt(wtd.var(awareness,weight_post,na.rm=T))) %>%
  filter(leftright!=2) %>% 
  ungroup %>% 
  mutate(leftright2=c(1,1,2,2))
aw

scep <- data %>%
  group_by(leftright,random) %>%
  summarise(mean=weighted.mean(sceptical, weight_post,na.rm=T),
            sd=sqrt(wtd.var(sceptical,weight_post,na.rm=T))) %>%
  filter(leftright!=2) %>% 
  ungroup %>% 
  mutate(leftright2=c(1,1,2,2))
scep

worry <- data %>%
  group_by(leftright,random) %>%
  summarise(mean=weighted.mean(worry, weight_post,na.rm=T),
            sd=sqrt(wtd.var(worry,weight_post,na.rm=T))) %>%
  filter(leftright!=2) %>% 
  ungroup %>% 
  mutate(leftright2=c(1,1,2,2))
worry

impact <- data %>%
  group_by(leftright,random) %>%
  summarise(mean=weighted.mean(impact, weight_post,na.rm=T),
            sd=sqrt(wtd.var(impact,weight_post,na.rm=T))) %>%
  filter(leftright!=2) %>% 
  ungroup %>% 
  mutate(leftright2=c(1,1,2,2))
impact

##running t tests
weighted_ttest(awareness ~ random +weight_post,subset(data,leftright==1))
weighted_ttest(awareness ~ random +weight_post,subset(data,leftright==3))
weighted_ttest(sceptical ~ random +weight_post,subset(data,leftright==1))
weighted_ttest(sceptical ~ random +weight_post,subset(data,leftright==3))
weighted_ttest(worry ~ random +weight_post,subset(data,leftright==1))
weighted_ttest(worry ~ random +weight_post,subset(data,leftright==3))
weighted_ttest(impact ~ random +weight_post,subset(data,leftright==1))
weighted_ttest(impact ~ random +weight_post,subset(data,leftright==3))

###plotting means

p1 <- ggplot(aw,aes(as.factor(leftright2),mean,fill=random,group=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.7) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",breaks=c(1:2),
                   labels=c("Leftists","Rightist"))+
  scale_y_continuous(limits = c(1, 5.5), breaks=c(1,2,3,4),
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"),labels=c("Global","Climate"),
                    name="Wording")+
  geom_text(x=1, y=5, label="p=.000",color="grey20",family="Times New Roman",size=4)+
  geom_text(x=2, y=5, label="p=.000",color="grey20",family="Times New Roman",size=4)+
  theme_minimal()+
  ggtitle("Existence belief")+
  theme(plot.title = element_text(hjust = 0.5),
        text=element_text(family="Times New Roman"),
        axis.text.y=element_text())
p1

p2 <- ggplot(scep,aes(as.factor(leftright2),mean,fill=random,group=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.7) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",breaks=c(1:2),
                   labels=c("Leftists","Rightist"))+
  scale_y_continuous(limits = c(0, 5.5), breaks=c(0,1,2,3,4,5),
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"),labels=c("Global","Climate"),
                    name="Wording")+
  geom_text(x=1, y=5.5, label="p=.325",color="grey20",family="Times New Roman",size=4)+
  geom_text(x=2, y=5.5, label="p=.182",color="grey20",family="Times New Roman",size=4)+
  theme_minimal()+
  ggtitle("Attribution skepticism")+
  theme(plot.title = element_text(hjust = 0.5),
        text=element_text(family="Times New Roman"),
        axis.text.y=element_text())
p2

p3 <- ggplot(worry,aes(as.factor(leftright2),mean,fill=random,group=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.7) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",breaks=c(1:2),
                   labels=c("Leftists","Rightist"))+
  scale_y_continuous(limits = c(1, 5.5), breaks=c(1,2,3,4,5),
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"),labels=c("Global","Climate"),
                    name="Wording")+
  geom_text(x=1, y=5, label="p=.582",color="grey20",family="Times New Roman",size=4)+
  geom_text(x=2, y=5, label="p=.551",color="grey20",family="Times New Roman",size=4)+
  theme_minimal()+
  ggtitle("Worry")+
  theme(plot.title = element_text(hjust = 0.5),
        text=element_text(family="Times New Roman"),
        axis.text.y=element_text())
p3

p4 <- ggplot(impact,aes(as.factor(leftright2),mean,fill=random,group=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.7) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",breaks=c(1:2),
                   labels=c("Leftists","Rightist"))+
  scale_y_continuous(limits = c(0, 11),breaks=c(0,2,4,6,8,10), 
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"),labels=c("Global","Climate"),
                    name="Wording")+
  geom_text(x=1, y=11, label="p=.815",color="grey20",family="Times New Roman",size=4)+
  geom_text(x=2, y=11, label="p=.038",color="grey20",family="Times New Roman",size=4)+
  theme_minimal()+
  ggtitle("Negative impact")+
  theme(plot.title = element_text(hjust = 0.5),
        text=element_text(family="Times New Roman"),
        axis.text.y=element_text())
p4
p<-grid.arrange(p1,p2,p3,p4,nrow=2)
p

ggsave("fig1_leftright.png",p,h=14,w=16,units="cm")
?ggsave

##calculating mean_sd between the conditions - green-right-wing voters

aw <- data %>%
  group_by(green_right,random) %>%
  summarise(mean=weighted.mean(awareness, weight_post,na.rm=T),
            sd=sqrt(wtd.var(awareness,weight_post,na.rm=T))) %>%
  filter(green_right!=0) %>% 
  ungroup %>% 
  mutate(greenright2=c(1,1,2,2))
aw

scep <- data %>%
  group_by(green_right,random) %>%
  summarise(mean=weighted.mean(sceptical, weight_post,na.rm=T),
            sd=sqrt(wtd.var(sceptical,weight_post,na.rm=T))) %>%
  filter(green_right!=2) %>% 
  ungroup %>% 
  mutate(greenright2=c(1,1,2,2))
scep

worry <- data %>%
  group_by(green_right,random) %>%
  summarise(mean=weighted.mean(worry, weight_post,na.rm=T),
            sd=sqrt(wtd.var(worry,weight_post,na.rm=T))) %>%
  filter(green_right!=2) %>% 
  ungroup %>% 
  mutate(greenright2=c(1,1,2,2))
worry

impact <- data %>%
  group_by(green_right,random) %>%
  summarise(mean=weighted.mean(impact, weight_post,na.rm=T),
            sd=sqrt(wtd.var(impact,weight_post,na.rm=T))) %>%
  filter(green_right!=2) %>% 
  ungroup %>% 
  mutate(greenright2=c(1,1,2,2))
impact

##running t tests
weighted_ttest(awareness ~ random +weight_post,subset(data,green_right==1),na.action=na.omit)
weighted_ttest(awareness ~ random +weight_post,subset(data,green_right==2),na.action=na.omit)
weighted_ttest(sceptical ~ random +weight_post,subset(data,green_right==1),na.action=na.omit)
weighted_ttest(sceptical ~ random +weight_post,subset(data,green_right==2),na.action=na.omit)
weighted_ttest(worry ~ random +weight_post,subset(data,green_right==1),na.action=na.omit)
weighted_ttest(worry ~ random +weight_post,subset(data,green_right==2),na.action=na.omit)
weighted_ttest(impact ~ random +weight_post,subset(data,green_right==1),na.action=na.omit)
weighted_ttest(impact ~ random +weight_post,subset(data,green_right==2),na.action=na.omit)

###plotting means

p1 <- ggplot(aw,aes(as.factor(greenright2),mean,fill=random,group=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.7) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",breaks=c(1:2),
                   labels=c("Green party","Right-wing"))+
  scale_y_continuous(limits = c(1, 5.5), breaks=c(1,2,3,4),
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"),labels=c("Global","Climate"),
                    name="Wording")+
  geom_text(x=1, y=5, label="p=.050",color="grey20",family="Times New Roman",size=4)+
  geom_text(x=2, y=5, label="p=.024",color="grey20",family="Times New Roman",size=4)+
  theme_minimal()+
  ggtitle("Existence belief")+
  theme(plot.title = element_text(hjust = 0.5),
        text=element_text(family="Times New Roman"),
        axis.text.y=element_text())
p1

p2 <- ggplot(scep,aes(as.factor(greenright2),mean,fill=random,group=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.7) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",breaks=c(1:2),
                   labels=c("Green party","Right-wing"))+
  scale_y_continuous(limits = c(0, 5.5), breaks=c(0,1,2,3,4,5),
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"),labels=c("Global","Climate"),
                    name="Wording")+
  geom_text(x=1, y=5.5, label="p=.111",color="grey20",family="Times New Roman",size=4)+
  geom_text(x=2, y=5.5, label="p=.121",color="grey20",family="Times New Roman",size=4)+
  theme_minimal()+
  ggtitle("Attribution skepticism")+
  theme(plot.title = element_text(hjust = 0.5),
        text=element_text(family="Times New Roman"),
        axis.text.y=element_text())
p2

p3 <- ggplot(worry,aes(as.factor(greenright2),mean,fill=random,group=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.7) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",breaks=c(1:2),
                   labels=c("Green party","Right-wing"))+
  scale_y_continuous(limits = c(1, 5.5), breaks=c(1,2,3,4,5),
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"),labels=c("Global","Climate"),
                    name="Wording")+
  geom_text(x=1, y=5, label="p=.151",color="grey20",family="Times New Roman",size=4)+
  geom_text(x=2, y=5, label="p=.966",color="grey20",family="Times New Roman",size=4)+
  theme_minimal()+
  ggtitle("Worry")+
  theme(plot.title = element_text(hjust = 0.5),
        text=element_text(family="Times New Roman"),
        axis.text.y=element_text())
p3

p4 <- ggplot(impact,aes(as.factor(greenright2),mean,fill=random,group=random))+
  geom_bar(position = position_dodge(0.9),stat='identity',width=.7) +
  geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd),width=0.1,position = position_dodge(width=.9),
                alpha=0.5)+
  scale_x_discrete(name="",breaks=c(1:2),
                   labels=c("Green party","Right-wing"))+
  scale_y_continuous(limits = c(0, 11),breaks=c(0,2,4,6,8,10), 
                     oob = scales::squish)+
  scale_fill_manual(values=c("grey80","grey40"),labels=c("Global","Climate"),
                    name="Wording")+
  geom_text(x=1, y=11, label="p=.277",color="grey20",family="Times New Roman",size=4)+
  geom_text(x=2, y=11, label="p=.127",color="grey20",family="Times New Roman",size=4)+
  theme_minimal()+
  ggtitle("Negative impact")+
  theme(plot.title = element_text(hjust = 0.5),
        text=element_text(family="Times New Roman"),
        axis.text.y=element_text())
p4
p<-grid.arrange(p1,p2,p3,p4,nrow=2)
p

ggsave("fig1_greenright.png",p,h=14,w=16,units="cm")

####calculating distributions between the conditions - full sample

aw <- data %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,awareness) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct))
aw
write_xlsx(aw,"aw.xlsx")

data$sceptical <- as.factor(data$sceptical)
scep <- data %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,sceptical) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct))
scep
write_xlsx(scep,"scep.xlsx")

data$worry <- as.factor(data$worry)
worry <- data %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,worry) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
worry
write_xlsx(worry,"worry.xlsx")

data$impact <- as.factor(data$impact)
impact <- data %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,impact) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
impact
write_xlsx(impact,"impact.xlsx")

##chi - full sample
weighted_chisqtest(data, awareness, random, weights = weight_post)
weighted_chisqtest(data, sceptical, random, weights = weight_post)
weighted_chisqtest(data, worry, random, weights = weight_post)
weighted_chisqtest(data, impact, random, weights = weight_post)

####calculating distributions between the conditions - left-right

aw <- subset(data,leftright==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,awareness) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct))
aw
write_xlsx(aw,"aw.xlsx")

scep <- subset(data,leftright==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,sceptical) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct))
scep
write_xlsx(scep,"scep.xlsx")


worry <- subset(data,leftright==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,worry) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
worry
write_xlsx(worry,"worry.xlsx")

impact <- subset(data,leftright==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,impact) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
impact
write_xlsx(impact,"impact.xlsx")

##chi - left
weighted_chisqtest(subset(data,leftright==1), awareness, random, weights = weight_post)
weighted_chisqtest(subset(data,leftright==1), sceptical, random, weights = weight_post)
weighted_chisqtest(subset(data,leftright==1), worry, random, weights = weight_post)
weighted_chisqtest(subset(data,leftright==1), impact, random, weights = weight_post)

##distribution - right

aw <- subset(data,leftright==3) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,awareness) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
aw
write_xlsx(aw,"aw.xlsx")

scep <- subset(data,leftright==3) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,sceptical) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
scep
write_xlsx(scep,"scep.xlsx")


worry <- subset(data,leftright==3) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,worry) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
worry
write_xlsx(worry,"worry.xlsx")

impact <- subset(data,leftright==3) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,impact) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
impact
write_xlsx(impact,"impact.xlsx")


##chi - right
weighted_chisqtest(subset(data,leftright==3), awareness, random, weights = weight_post)
weighted_chisqtest(subset(data,leftright==3), sceptical, random, weights = weight_post)
weighted_chisqtest(subset(data,leftright==3), worry, random, weights = weight_post)
weighted_chisqtest(subset(data,leftright==3), impact, random, weights = weight_post)



####calculating distributions between the conditions - green-right-wing voter

aw <- subset(data,green_right==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,awareness) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
aw
write_xlsx(aw,"aw.xlsx")

scep <- subset(data,green==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,sceptical) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
scep
write_xlsx(scep,"scep.xlsx")


worry <- subset(data,green==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,worry) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
worry
write_xlsx(worry,"worry.xlsx")

impact <- subset(data,green==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,impact) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
impact
write_xlsx(impact,"impact.xlsx")


##chi - green
weighted_chisqtest(subset(data,green==1), awareness, random, weights = weight_post)
weighted_chisqtest(subset(data,green==1), sceptical, random, weights = weight_post)
weighted_chisqtest(subset(data,green==1), worry, random, weights = weight_post)
weighted_chisqtest(subset(data,green==1), impact, random, weights = weight_post)


##distribution - right-wing

aw <- subset(data,rightwing==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,awareness) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
aw
write_xlsx(aw,"aw.xlsx")

scep <- subset(data,rightwing==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,sceptical) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
scep
write_xlsx(scep,"scep.xlsx")


worry <- subset(data,rightwing==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,worry) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
worry
write_xlsx(worry,"worry.xlsx")

impact <- subset(data,rightwing==1) %>%
  as_survey(weights = c(weight_post)) %>%
  group_by(random,impact) %>%
  summarise(n=survey_total()) %>%
  mutate(pct=n/sum(n)) %>% 
  mutate(label=scales::percent(pct,accuracy = 0.01))
impact
write_xlsx(impact,"impact.xlsx")


##chi - right-wing
weighted_chisqtest(subset(data,rightwing==1), awareness, random, weights = weight_post)
weighted_chisqtest(subset(data,rightwing==1), sceptical, random, weights = weight_post)
weighted_chisqtest(subset(data,rightwing==1), worry, random, weights = weight_post)
weighted_chisqtest(subset(data,rightwing==1), impact, random, weights = weight_post)

####calculating differences in means between the conditions and t-tests - per country

##awareness

aw1<- data %>% ##calculating mean diff.
  as_survey(weights = c(weight_post)) %>%
  group_by(cntry,random) %>%
  summarise(survey_mean(awareness, na.rm=T)) %>%
  select(-'_se')

aw1_wide<- spread(aw1, random,coef) ##transforming to wide format
aw1_wide <- aw1_wide %>%
  mutate(diff=`0`-`1`)

aw2 <- data %>% ##calculating p-values of t-tests
  group_by(cntry) %>%
  filter(!is.na(awareness)) %>% 
  do(tidy(lm(awareness ~ random,data=.,weight=weight_post))) %>% 
  filter(term=="random1") %>% 
  select(cntry,p.value) %>% 
  mutate(sig=case_when(p.value<0.05 ~ 1,
                       p.value>=0.05 ~0))

##plotting awareness
p<-bind_cols(aw1_wide,aw2) %>% 
ggplot(aes(reorder(cntry...1,diff),diff,fill=as.factor(sig)))+
  geom_bar(stat="identity", 
           position=position_dodge())+
  geom_hline(yintercept=0,linetype="dashed")+
  scale_y_continuous(name="Difference in means")+
  scale_x_discrete(name="",labels=cntry_labels)+
  scale_fill_manual(values=c("grey80","grey40"))+
  ggtitle("Existence belief")+
  theme_minimal()+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5,size=12),
        text=element_text(family="Times New Roman",size=17),
        axis.text.y=element_text(size=10),
        axis.text.x=element_text(size=8),
        axis.title=element_text(size=12))+coord_flip()

p
ggsave("aw_country.png",p,h=11,w=12,units="cm")


##scep

scep1 <- data %>% ##calculating mean diff.
  as_survey(weights = c(weight_post)) %>%
  group_by(cntry,random) %>%
  summarise(survey_mean(sceptical, na.rm=T)) %>%
  select(-'_se')

scep1_wide<- spread(scep1, random,coef) ##transforming to wide format
scep1_wide <- scep1_wide %>%
  mutate(diff=`0`-`1`)

View(scep1_wide)


scep2 <- data %>%  ##calculating p-values of t-tests
  group_by(cntry) %>%
  filter(!is.na(sceptical)) %>% 
  do(tidy(lm(sceptical ~ random,data=.,weight=weight_post))) %>% 
  filter(term=="random1") %>% 
  select(cntry,p.value) %>% 
  mutate(sig=case_when(p.value<0.05 ~ 1,
                       p.value>=0.05 ~0))

##attribution skepticism
p<-bind_cols(scep1_wide,scep2) %>% 
  ggplot(aes(reorder(cntry...1,diff),diff,fill=as.factor(sig)))+
  geom_bar(stat="identity", 
           position=position_dodge())+
  geom_hline(yintercept=0,linetype="dashed")+
  scale_y_continuous(name="Difference in means")+
  scale_x_discrete(name="",labels=cntry_labels)+
  scale_fill_manual(values=c("grey80","grey40"))+
  ggtitle("Attribution skepticism")+
  theme_minimal()+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5,size=12),
        text=element_text(family="Times New Roman",size=17),
        axis.text.y=element_text(size=10),
        axis.text.x=element_text(size=8),
        axis.title=element_text(size=12))+coord_flip()

ggsave("scep_country.png",p,h=11,w=12,units="cm")

##worry

worry1<- data %>% ##calculating mean diff.
  as_survey(weights = c(weight_post)) %>%
  group_by(cntry,random) %>%
  summarise(survey_mean(worry, na.rm=T)) %>%
  select(-'_se')

worry1_wide<- spread(worry1, random,coef) ##transforming to wide format
worry1_wide <- worry1_wide %>%
  mutate(diff=`0`-`1`)

worry2 <- data %>% ##calculating p-values of t-tests
  group_by(cntry) %>%
  filter(!is.na(worry)) %>% 
  do(tidy(lm(worry ~ random,data=.,weight=weight_post))) %>% 
  filter(term=="random1") %>% 
  select(cntry,p.value) %>% 
  mutate(sig=case_when(p.value<0.05 ~ 1,
                       p.value>=0.05 ~0))

##plotting worry
p<-bind_cols(worry1_wide,worry2) %>% 
  ggplot(aes(reorder(cntry...1,diff),diff,fill=as.factor(sig)))+
  geom_bar(stat="identity", 
           position=position_dodge())+
  geom_hline(yintercept=0,linetype="dashed")+
  scale_y_continuous(name="Difference in means")+
  scale_x_discrete(name="",labels=cntry_labels)+
  scale_fill_manual(values=c("grey80","grey40"))+
  ggtitle("Worry")+
  theme_minimal()+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5,size=12),
        text=element_text(family="Times New Roman",size=17),
        axis.text.y=element_text(size=10),
        axis.text.x=element_text(size=8),
        axis.title=element_text(size=12))+coord_flip()
p
ggsave("worry_country.png",p,h=11,w=12,units="cm")



##impact

impact1<- data %>% ##calculating mean diff.
  as_survey(weights = c(weight_post)) %>%
  group_by(cntry,random) %>%
  summarise(survey_mean(impact, na.rm=T)) %>%
  select(-'_se')

impact1_wide<- spread(impact1, random,coef) ##transforming to wide format
impact1_wide <- impact1_wide %>%
  mutate(diff=`0`-`1`)

impact2 <- data %>% ##calculating p-values of t-tests
  group_by(cntry) %>%
  filter(!is.na(impact)) %>% 
  do(tidy(lm(impact ~ random,data=.,weight=weight_post))) %>% 
  filter(term=="random1") %>% 
  select(cntry,p.value) %>% 
  mutate(sig=case_when(p.value<0.05 ~ 1,
                       p.value>=0.05 ~0))

##plotting impact
p<- bind_cols(impact1_wide,impact2) %>% 
  ggplot(aes(reorder(cntry...1,diff),diff,fill=as.factor(sig)))+
  geom_bar(stat="identity", 
           position=position_dodge())+
  geom_hline(yintercept=0,linetype="dashed")+
  scale_y_continuous(name="Difference in means")+
  scale_x_discrete(name="",labels=cntry_labels)+
  scale_fill_manual(values=c("grey80","grey40"))+
  ggtitle("Perceived impact")+
  theme_minimal()+
  theme(legend.position = "none",plot.title = element_text(hjust = 0.5,size=12),
        text=element_text(family="Times New Roman",size=17),
        axis.text.y=element_text(size=10),
        axis.text.x=element_text(size=8),
        axis.title=element_text(size=12))+coord_flip()
p
ggsave("impact_country.png",p,h=11,w=12,units="cm")



###plotting google trends results

data_google <- read.table("googletrends_climate_global.csv",sep=",",header=T)

data_long<- reshape::melt(data_google, id.vars=c("Month"))

p <- data_long %>%
  ggplot()+
  geom_line(aes(x=as.factor(Month),value,group=variable,color=variable))+
  scale_color_manual(values=c("grey20","grey80"),name="",labels=c("Climate change",
                                                                  "Global warming"))+
  scale_y_continuous(name="Interest point")+
  scale_x_discrete(name="",breaks=c("2004-01","2005-01","2006-01","2007-01","2008-01",
                                    "2009-01","2010-01",
                                    "2011-01","2012-01","2013-01","2014-01","2015-01",
                                    "2016-01","2017-01","2018-01","2019-01","2020-01",
                                    "2021-01","2022-01"),labels=c("2004",
                                                                  "2005",
                                                                  "2006",
                                                                  "2007",
                                                                  "2008",
                                                                  "2009",
                                                                  "2010",
                                                                  "2011",
                                                                  "2012",
                                                                  "2013",
                                                                  "2014",
                                                                  "2015",
                                                                  "2016",
                                                                  "2017",
                                                                  "2018",
                                                                  "2019",
                                                                  "2020",
                                                                  "2021",
                                                                  "2022")) +
  theme_minimal()+
  theme(text=element_text(family="Times New Roman",size=17))
p  
ggsave("trend.png",p,w=12)

#####multilevel models

model1 <- lme4::lmer(awareness ~ random + (1 | cntry), data,weights=weight_post)
model2 <- lme4::lmer(sceptical ~ random + (1 | cntry), data,weights=weight_post)
model3 <- lme4::lmer(worry ~ random + (1 | cntry), data,weights=weight_post)
model4 <- lme4::lmer(impact ~ random + (1 | cntry), data,weights=weight_post)

tab_model(model1,model2,model3,model4,show.ci = F,
                  show.se = T,
                  pred.labels=c("Intercept",
                                "Wording group (ref.: Climate wording)"),
                 dv.labels = c("Existence belief","Attribution scepticism",
                               "Worry","Negative impact"),file="full.doc")

##left
model1 <- lme4::lmer(awareness ~ random + (1 | cntry), subset(data,leftright==1),
                     weights=weight_post)
model2 <- lme4::lmer(sceptical ~ random + (1 | cntry), subset(data,leftright==1),
                     weights=weight_post)
model3 <- lme4::lmer(worry ~ random + (1 | cntry), subset(data,leftright==1),
                     weights=weight_post)
model4 <- lme4::lmer(impact ~ random + (1 | cntry), subset(data,leftright==1),
                     weights=weight_post)

tab_model(model1,model2,model3,model4,show.ci = F,
          show.se = T,
          pred.labels=c("Intercept",
                        "Wording group (ref.: Climate wording)"),
          dv.labels = c("Existence belief","Attribution scepticism",
                        "Worry","Negative impact"),file="left.doc")


##right
model1 <- lme4::lmer(awareness ~ random + (1 | cntry), subset(data,leftright==3),
                     weights=weight_post)
model2 <- lme4::lmer(sceptical ~ random + (1 | cntry), subset(data,leftright==3),
                     weights=weight_post)
model3 <- lme4::lmer(worry ~ random + (1 | cntry), subset(data,leftright==3),
                     weights=weight_post)
model4 <- lme4::lmer(impact ~ random + (1 | cntry), subset(data,leftright==3),
                     weights=weight_post)

tab_model(model1,model2,model3,model4,show.ci = F,
          show.se = T,
          pred.labels=c("Intercept",
                        "Wording group (ref.: Climate wording)"),
          dv.labels = c("Existence belief","Attribution scepticism",
                        "Worry","Negative impact"),file="right.doc")

##green
model1 <- lme4::lmer(awareness ~ random + (1 | cntry), subset(data,green_right==1),
                     weights=weight_post)
model2 <- lme4::lmer(sceptical ~ random + (1 | cntry), subset(data,green_right==1),
                     weights=weight_post)
model3 <- lme4::lmer(worry ~ random + (1 | cntry), subset(data,green_right==1),
                     weights=weight_post)
model4 <- lme4::lmer(impact ~ random + (1 | cntry), subset(data,green_right==1),
                     weights=weight_post)

tab_model(model1,model2,model3,model4,show.ci = F,
          show.se = T,
          pred.labels=c("Intercept",
                        "Wording group (ref.: Climate wording)"),
          dv.labels = c("Existence belief","Attribution scepticism",
                        "Worry","Negative impact"),file="green.doc")

##rightwing
model1 <- lme4::lmer(awareness ~ random + (1 | cntry), subset(data,green_right==2),
                     weights=weight_post)
model2 <- lme4::lmer(sceptical ~ random + (1 | cntry), subset(data,green_right==2),
                     weights=weight_post)
model3 <- lme4::lmer(worry ~ random + (1 | cntry), subset(data,green_right==2),
                     weights=weight_post)
model4 <- lme4::lmer(impact ~ random + (1 | cntry), subset(data,green_right==2),
                     weights=weight_post)

tab_model(model1,model2,model3,model4,show.ci = F,
          show.se = T,
          pred.labels=c("Intercept",
                        "Wording group (ref.: Climate wording)"),
          dv.labels = c("Existence belief","Attribution scepticism",
                        "Worry","Negative impact"),file="rwing.doc")

jtools::summ(model1)
